<%@ page language="java" contentType="text/html; charset=utf-8"
		 pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<link href="../css/eys_stly.css" rel="stylesheet" type="text/css"/>
	<link href="../css/bootstrap.css" rel="stylesheet" type="text/css"/>
	<link href="../ss/css/bootstrap-table.css" rel="stylesheet" type="text/css"/>

</head>
<body>

<div class="index2">
	<h5>所在位置：<a>销售管理-销售类型管理</a></h5>

	<form  id="search">
		<p class="p_line">
			<input type="text" name="code"   class="form-control" value="" placeholder="编码"/>
			<input type="text" name="name"   class="form-control" value="" placeholder="名称"/>

			<a class=" button_style2" onclick="searchData(this)">查询</a>

		</p>
	</form>
	<p><button class="create button_style2">添加</button>
		<span class="alert"></span></p>

	<%-- <p>
         <button class="button_style2">批量导出</button>
     </p>--%>
	<p>

	<table   id="table" style="margin: 0 auto">
	</table>
	</p>

</div>

</div>
<div id="modal" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title"></h4>
			</div>
			<div class="modal-body">
				<form  class="form-horizontal m" id="form-add"  >
					<input type="hidden"  name="id"  >

					<div class="form-group">
						<label>编码</label>
						<input type="text" class="form-control" name="code" placeholder="编码">
					</div>
					<div class="form-group">
						<label>名称</label>
						<input type="text" class="form-control" name="name" placeholder="名称">
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary submit">提交</button>
			</div>
		</div>
	</div>
	<script src="../js/jquery.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../ss/js/bootstrap-table.js"></script>
	<script src="../ss/js/bootstrap-table-zh-CN.min.js"></script>
	<script type="text/javascript">
        var $modal = $('#modal').modal({show: false})
        var $alert = $('.alert').hide()
        var $table ;
        $(function () {
            $('.create').click(function () {
                $('#form-add')[0].reset();
                $('#form-add input[name="id"]').val("");
                showModal($(this).text())
            })
            $table=$('#table').bootstrapTable({
                method: "post",
                striped: true,
                singleSelect: false,
                url: "../xsd_xsglSaleType/listData",
                pagination: true, //分页
                pageSize: 10,
                pageNumber: 1,
                queryParamsType:'',
                queryParams: searchParam,
                sidePagination: "server",      //分页方式：client客户端分页，server服务端分页（*）
                search: false, //显示搜索框
                contentType: "application/x-www-form-urlencoded",

                columns: [

                    {
                        title: "编码",
                        field: 'code',
                        align: 'center',
                        valign: 'middle'
                    },
                    {
                        title: "名称",
                        field: 'name',
                        align: 'center',
                        valign: 'middle'
                    },
                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: actionFormatter,
                        events:actionEvents
                    }
                ]
            });

            $modal.find('.submit').click(function () {
                /* var row = {}
                 $modal.find('input[name]').each(function () {
                     row[$(this).attr('name')] = $(this).val()
                 })*/
                var dataFormJson = $("#form-add").serialize();
                console.log(dataFormJson);
                $.ajax({
                    cache : true,
                    type : "POST",
                    url: "../xsd_xsglSaleType/save",
                    data : dataFormJson,
                    async : false,
                    error : function(request) {
                        $modal.modal('hide')
                        showAlert(($modal.data('id') ? '修改' : '添加') + '失败!', 'danger')
                    },
                    success : function(data) {
                        $modal.modal('hide')
                        $('#form-add')[0].reset();
                        showAlert(($modal.data('id') ? '修改' : '添加') + '成功!', 'success')

                    }
                });

            })

        })
        function showModal(title, row_) {
            var row = row_ || {
                id: '',
                name: '',
                code: ''
            }
            $modal.data('id', row.id)
            $modal.find('.modal-title').text(title)
            var fom = $("#form-add");
            for (var name in row) {
                if (row[name]) {
                    //$('#form-add input[name="'+name+']').val(row[name]);
                    $modal.find('input[name="' + name + '"]').val(row[name])
                    //$modal.find('select[name="' + name + '"]').val(row[name])
                }
            }
            $modal.modal('show');
        }
        function searchParam(params) {
            // var form=$(my).parents("form");
            var fom = $("#search");
            //var inpt = fom.find("input:checked");
            var params2 = {
                pageNo: params.pageNumber,//当前页(开始页)
                pageSize: params.pageSize,//每页的数量
                code:$("#search input[name=code]").val(),
                name: $("#search input[name=name]").val()
            };
            return params2;
        }
        function searchData(my){
            var form=$(my).parents("form");
            var search = {};
            $.each(form.serializeArray(), function(i, field) {
                search[field.name] = field.value;
            });
            var params = $("#table").bootstrapTable("getOptions");
            params.queryParams=function(params){

                search.pageNo= params.pageSize;
                search.pageNum=params.pageNumber;
                return search;
            };
            $("#table").bootstrapTable("refresh", params);

        }
        function showAlert(title, type) {
            $table.bootstrapTable('refresh')
            $alert.attr('class', 'alert alert-' + type || 'success')
                .html( title).show();
            setTimeout(function () {
                $alert.hide()
            }, 3000)
        }

        window.actionEvents = {
            'click .update': function (e, value, row) {
                console.log(row);
                showModal($(this).attr('title'), row)
            },
            'click .remove': function (e, value, row) {
                if (confirm('确定要删除吗?')) {
                    $.ajax({
                        url: "../xsd_xsglSaleType/delete?id="+row.id,
                        type: 'post',
                        success: function () {

                            showAlert('删除成功!', 'success')

                        },
                        error: function () {
                            showAlert('删除失败', 'danger')

                        }
                    })
                }
            }
        }

        function actionFormatter(value) {
            return [
                '<a class="update button_style2" href="javascript:" title="修改">修改</a>',
                '<a class="remove button_style2" href="javascript:" title="删除">删除</a>'
            ].join('')
        }
	</script>
</body>
</html>